package com.ciquan.server.controller;

import com.ciquan.server.bean.Advertisement;
import com.ciquan.server.service.AdvertisementService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by kakalee on 15/3/27.
 */
@Controller
@RequestMapping("advertisement")
public class AdvertisementController extends AbstractController {

    @Resource
    private AdvertisementService advertisementService;

    @RequestMapping("list")
    public ModelAndView list(ModelMap modelMap) {
        List<Advertisement> advertisements = advertisementService.selectList();
        modelMap.put("advertisements", advertisements);
        return new ModelAndView("advertisementList", modelMap);
    }

    @RequestMapping("add")
    public ModelAndView add() {
        return new ModelAndView("addAdvertisement");
    }

    @RequestMapping("saveAdd")
    public ModelAndView saveAdd(HttpServletRequest request, @RequestParam(value = "file") MultipartFile file) {
        String title = request.getParameter("title");
        String url = request.getParameter("url");
        int type = ServletRequestUtils.getIntParameter(request, "type", 1);

        String path = request.getSession().getServletContext().getRealPath("upload");
        File f = new File(path);
        if(!f.exists()) {
            f.mkdir();
        }

        f = new File(f, String.valueOf(System.currentTimeMillis()) + ".jpg");
        try {
            file.transferTo(f);
        } catch (IOException e) {
            logger.error("IOException", e);
        }

        Advertisement advertisement = new Advertisement();
        advertisement.setTitle(title);
        advertisement.setUrl(url);
        advertisement.setImage(f.getName());
        advertisement.setType(type);
        advertisementService.insert(advertisement);
        return new ModelAndView("redirect:/advertisement/list");
    }

    @RequestMapping("edit")
    public ModelAndView edit(HttpServletRequest request, ModelMap modelMap) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        Advertisement advertisement = advertisementService.selectById(id);
        modelMap.put("advertisement", advertisement);
        return new ModelAndView("editAdvertisement", modelMap);
    }

    @RequestMapping("saveEdit")
    public ModelAndView saveEdit(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        String title = request.getParameter("title");
        String url = request.getParameter("url");
        int type = ServletRequestUtils.getIntParameter(request, "type", 1);
        Advertisement advertisement = new Advertisement();
        advertisement.setId(id);
        advertisement.setTitle(title);
        advertisement.setUrl(url);
        advertisement.setType(type);

        if (file != null && !file.isEmpty()) {
            String path = request.getSession().getServletContext().getRealPath("upload");
            File f = new File(path);
            if(!f.exists()) {
                f.mkdir();
            }

            f = new File(f, String.valueOf(System.currentTimeMillis()) + ".jpg");
            try {
                file.transferTo(f);
            } catch (IOException e) {
                logger.error("IOException", e);
            }
            advertisement.setImage(f.getName());
        }
        advertisementService.update(advertisement);
        return new ModelAndView("redirect:/advertisement/list");
    }

}
